﻿using System;
using System.Linq;
using Csla.Data;
using Csla;

namespace Accounting.Library.Backup
{
    public class BackupBSP_BanDichVu : BackupService
    {
        public BackupBSP_BanDichVu(object ChungTu) : base(ChungTu) { }

        public override void BackupData(ContextManager<Accounting.DataAccess.AccountingDataContext> ctx, BackupMode backupMode)
		{
			//Backup BSP_BanDichVu data
			DataAccess.BSP_BanDichVu parent = (DataAccess.BSP_BanDichVu)ChungTu;

			DataAccess.BSP_BanDichVu_Bak backUpData = new DataAccess.BSP_BanDichVu_Bak();
			backUpData.ChungTuBanDau = parent.Khoa;
			backUpData.Ma = parent.Ma;
			backUpData.NgayChungTu = parent.NgayChungTu;
			backUpData.NgayHachToan = parent.NgayHachToan;
			backUpData.NhaCungCapId = parent.NhaCungCapId;
			backUpData.PhuongThucThanhToanId = parent.PhuongThucThanhToanId;
			backUpData.LoaiTienId = parent.LoaiTienId;
			backUpData.TyGia = parent.TyGia;
			backUpData.LoaiHDId = parent.LoaiHDId;
			backUpData.NgayHD = parent.NgayHD;
			backUpData.KHHoaDon = parent.KHHoaDon;
			backUpData.SoHD = parent.SoHD;
			backUpData.TongTienHang = parent.TongTienHang;
			backUpData.TienChietKhau = parent.TienChietKhau;
			backUpData.TienThueGTGT = parent.TienThueGTGT;
			backUpData.TongTienThanhToan = parent.TongTienThanhToan;
			backUpData.TongTienDaThanhToan = parent.TongTienDaThanhToan;
			backUpData.DienGiai = parent.DienGiai;
			backUpData.LaChiPhiMuaHang = parent.LaChiPhiMuaHang;
			backUpData.HoaDonBanHangId = parent.HoaDonBanHangId;
			backUpData.LoaiChungTuId = parent.LoaiChungTuId;

			//Insert Log data
			if (backupMode == BackupMode.Edit)
				backUpData.ThaoTac = BusinessConfiguration.Configuration.ThaoTac_ChinhSua;
			else if (backupMode == BackupMode.Delete)
				backUpData.ThaoTac = BusinessConfiguration.Configuration.ThaoTac_Xoa;
			backUpData.NhanVienThaoTac = ((Authentication.CustomIdentity)ApplicationContext.User.Identity).ID;
			try
			{
				int lanThaoTac = (int)(from p in ctx.DataContext.BSP_BanDichVu_Baks
				                       where (p.ChungTuBanDau == parent.Khoa)
				                       select p.SoLanThaoTac).Max();
				backUpData.SoLanThaoTac = lanThaoTac + 1;
			}
			catch (Exception)
			{
				backUpData.SoLanThaoTac = 1;
			}
			ctx.DataContext.BSP_BanDichVu_Baks.InsertOnSubmit(backUpData);
			ctx.DataContext.SubmitChanges();

			//Backup BSP_BanDichVu_ChiTiet data
			foreach (DataAccess.BSP_BanDichVu_ChiTiet child in parent.BSP_BanDichVu_ChiTiets)
			{
				DataAccess.BSP_BanDichVu_ChiTiet_Bak childBackUpdata = new DataAccess.BSP_BanDichVu_ChiTiet_Bak();
				childBackUpdata.ChungTuGocId = backUpData.Khoa;
				childBackUpdata.HangHoaId = child.HangHoaId;
				childBackUpdata.DienGiai = child.DienGiai;
				childBackUpdata.TKNoId = child.TKNoId;
				childBackUpdata.TKCoId = child.TKCoId;
				childBackUpdata.DVTId = child.DVTId;
				childBackUpdata.SoLuong = child.SoLuong;
				childBackUpdata.DonGia = child.DonGia;
				childBackUpdata.DonGiaQĐ = child.DonGiaQĐ;
				childBackUpdata.ThanhTien = child.ThanhTien;
				childBackUpdata.ThanhTienQĐ = child.ThanhTienQĐ;
				childBackUpdata.TyLeCK = child.TyLeCK;
				childBackUpdata.TienCK = child.TienCK;
				childBackUpdata.TienCKQĐ = child.TienCKQĐ;
				ctx.DataContext.BSP_BanDichVu_ChiTiet_Baks.InsertOnSubmit(childBackUpdata);
			}

			//Backup BSP_BanDichVu_Thue data
			foreach (DataAccess.BSP_BanDichVu_Thue child in parent.BSP_BanDichVu_Thues)
			{
				DataAccess.BSP_BanDichVu_Thue_Bak childBackUpdata = new DataAccess.BSP_BanDichVu_Thue_Bak();
				childBackUpdata.ChungTuGocId = backUpData.Khoa;
				childBackUpdata.HangHoaId = child.HangHoaId;
				childBackUpdata.DienGiai = child.DienGiai;
				childBackUpdata.ThueSuatId = child.ThueSuatId;
				childBackUpdata.TienThueGTGT = child.TienThueGTGT;
				childBackUpdata.TienThueGTGTQĐ = child.TienThueGTGTQĐ;
				childBackUpdata.TKNoId = child.TKNoId;
				childBackUpdata.TKCoId = child.TKCoId;
				ctx.DataContext.BSP_BanDichVu_Thue_Baks.InsertOnSubmit(childBackUpdata);
			}

			ctx.DataContext.SubmitChanges();
		}
    }
}
